import { createStore } from "vuex";

const { context, modules } = loadModules();
const store = createStore({ modules });
module.hot && module.hot.accept(context.id, update);
function loadModules() {
  const context = require.context("@/store/modules", true, /\.js|.ts$/);
  const modulesFiles = context.keys();
  const config = modulesFiles.map((key) => ({ key, name: key.match(/^\.\/(.*)\.\w+$/i)[1] }));
  const modules = config.reduce( (cur, { key, name }) => ({ ...cur, [name]: context(key).default }), {});
  return { context, modules };
}
function update() {
  const { modules } = loadModules();
  store.hotUpdate({ modules });
}

export default store;
